/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import models.dados.JdbcDAO;

/**
 *
 * @author Wenderson
 */
public class JdbcConnection {
   private String user = null;
   private String password = null;
   private String url = null;
   private String database = null;
   
   private static Connection connection = null;

   private void setPropriedades() {
       Properties properties = new Properties();
       
       properties.setProperty("jdbc.user", "sa");
       properties.setProperty("jdbc.password", "manager");
       properties.setProperty("jdbc.url", "jdbc:sqlserver://localhost:1433;databaseName");
       properties.setProperty("jdbc.database", "FrameWork");
       
       try {
           FileOutputStream fos = new FileOutputStream("configDB.xml");
           properties.storeToXML(fos, "FILE JDBC PROPERTIES:", "ISO-8859-1");
           fos.close();
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
   }
   
   public static void setPropriedades(String user, String password, String database) {
       Properties properties = new Properties();
       
       properties.setProperty("jdbc.user", user);
       properties.setProperty("jdbc.password", password);
       properties.setProperty("jdbc.url", "jdbc:sqlserver://localhost:1433;databaseName");
       properties.setProperty("jdbc.database", database);
       
       try {
           FileOutputStream fos = new FileOutputStream("configDB.xml");
           properties.storeToXML(fos, "FILE JDBC PROPERTIES:", "ISO-8859-1");
           fos.close();
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
   }
   
   private void getPropriedades() {
       Properties properties = new Properties();
       
       try {
           FileInputStream fis = new FileInputStream("configDB.xml");
           properties.loadFromXML(fis);
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
       
       this.setUser(properties.getProperty("jdbc.user"));
       this.setPassword(properties.getProperty("jdbc.password"));
       this.setUrl(properties.getProperty("jdbc.url"));
       this.setDatabase(properties.getProperty("jdbc.database"));
   }
   
   public static Connection getConnection() {
       try {
        if (JdbcConnection.connection == null) {
            JdbcConnection.connection = JdbcConnection.getConnectionImp();
        }
       } catch (Exception e) {
           LogErro.gravaErro(e);
           e.printStackTrace();
       }
       
       return JdbcConnection.connection;
   }
   
   private static Connection getConnectionImp() {
       Connection conn = null;
       
       try {
           JdbcConnection jdbc = new JdbcConnection();
           jdbc.getPropriedades();
           
           if (jdbc.getUser() == null || jdbc.getUser().equalsIgnoreCase("")) {
               jdbc.setPropriedades();
               jdbc.getPropriedades();
           }
           
           conn = DriverManager.getConnection(jdbc.getUrl() + "=" + jdbc.getDatabase(), jdbc.getUser(), jdbc.getPassword());
           
           return conn;
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
       
       return null;
   }
   
   public static boolean conexaoValida(String user, String password, String database) {
       try {
           Connection conn = 
                   DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=" + database, user, password);
           
           if (conn != null) {
               conn.close();
               return true;
           }
       } catch (Exception e) {
           LogErro.gravaErro(e);e.printStackTrace();
       }
       
       return false;
   }
   
   public static void main(String[] args) {
       if (new JdbcConnection().getConnection() != null) {
           System.out.println("Conectado");
       } else {
           System.out.println("Não Conectado");
       }
       
       int id = new JdbcDAO().nextId("BD_CONCESSIONARIA.SCH_DW", "DIM_MONTADORA");
       System.out.println(id);
   }

   //<editor-fold defaultstate="collapsed" desc="Gets e Sets">
   public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }
    //</editor-fold>
}
